'use client'
import './index.scss'
import Cookies from 'js-cookie';
import { Button, Form, Input, message, Flex, Space } from 'antd';
import { useRouter } from 'next/navigation';
import { LockOutlined, UserOutlined } from '@ant-design/icons'
import keyStore from '@/app/store/key';
import Active from '@/app/store/active'
import { useEffect } from 'react';
import { debounce } from '@/app/hook/limitation'
import { login, decrypt } from '@/app/api/webapi/index'


export default function page() {
    type FieldType = {
        username?: string;
        password?: string;
        remember?: string;
    };

    const router = useRouter();
    const { setUser } = keyStore()
    const { platform_img, company_img, company_background, text, dom } = Active()

    useEffect(() => {
        Getlogin()
    }, [])

    const Getlogin = async () => {
        try {
            if (typeof window != 'undefined') {
                if (window.location.search != '') {
                    const urllist = window.location.search.split('?')[1].split('&')
                    const dataurl: any = {}
                    urllist.forEach((item) => {
                        const objlist: any = item.split('=')
                        dataurl[objlist[0]] = objlist[1]
                    })
                    const data: any = await decrypt(dataurl)
                    if (data.code == 200) {
                        setUser(data.data.username);
                        Cookies.set('access_token', data.data.username, { path: '/' });
                        localStorage.setItem('access_token', data.data.username);
                        localStorage.setItem('refresh_token', data.data.username);
                        router.push('/pages/home');
                    } else {
                        message.error(data.msg)
                    }
                }
            }
        } catch {

        }
    }

    const originalOnFinish = async (values: any) => {  
            setUser(values.username);
            Cookies.set('access_token', values.username, { path: '/' });
            localStorage.setItem('access_token', values.username);
            localStorage.setItem('refresh_token', values.username);
            message.success('登录成功');
            router.push('/pages/home'); 
    }

    const onFinish = debounce(originalOnFinish, 500);

    return (
        <div className="login" style={{ backgroundImage: company_background }}>
            <div className='logindiv'>
                <div className='login_logo'>

                    <div className='private'>
                        <img src={company_img} alt="" />
                    </div>
                    <span>+</span>
                    <div className='platform_img'>
                        <img src={platform_img} alt="" />
                    </div>
                </div>
                <div className='logo_text'>
                    {text}
                </div>
                <Form
                    name="basic"
                    labelCol={{ span: 6 }}
                    wrapperCol={{ span: 16 }}
                    initialValues={{ remember: true }}
                    onFinish={onFinish}
                    autoComplete="off"
                >
                    <Form.Item<FieldType>
                        label="账号"
                        name="username"
                        rules={[
                            { required: true, message: '账号不可为空' }
                        ]}>
                        <Input prefix={<UserOutlined />} placeholder="账号" />
                    </Form.Item>

                    <Form.Item<FieldType>
                        label="密码"
                        name="password"
                        rules={[{ required: true, message: '密码不可为空' }]}
                    >
                        <Input.Password
                            prefix={<LockOutlined />}
                            placeholder="密码"
                        />
                    </Form.Item>
                    <Form.Item label={null}>
                        <Button block type="primary" htmlType="submit" >
                            登录
                        </Button>
                    </Form.Item>
                </Form>
            </div>
        </div>
    )
}
